package com.heima.article.kafkaListener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

@Component
public class ArticleListener {

    @Autowired(required = false)
    ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(String message){
        if(!StringUtils.isEmpty(message)){
            Map map = JSON.parseObject(message, Map.class);
            Long articleId = (Long)map.get("articleId");
            Integer enable = (Integer)map.get("enable");
            ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleId));
            if(apArticleConfig!=null){
                if(enable==1){
                    apArticleConfig.setIsDown(false);
                }else {
                    apArticleConfig.setIsDown(true);
                }
            }
            apArticleConfigMapper.updateById(apArticleConfig);

        }

    }
}